<?php

namespace App\Admin\Controllers;

use App\Models\News;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use App\Admin\Actions\NewsAction\NewsExamine;
use App\Admin\Actions\NewsAction\NewsTranslate;
use App\Admin\Actions\NewsAction\NewsBriefCreate;
use App\Admin\Actions\NewsAction\NewsExamineNone;
use App\Admin\Actions\NewsAction\BatchNewsExamine;
use App\Admin\Actions\NewsAction\BatchTranslate;

class NewsController extends AdminController
{
    protected $title = '新闻页面';
    

    protected function grid()
    {
        $grid = new Grid(new News());
		$grid->model()->orderByDesc('id')->paginate(50);
	    $grid->disableExport();
	    
	    $grid->column('title_cn', '中文标题');
	    $grid->column('brief', '中文摘要');
	    $grid->column('brief_zn', '英文摘要');
	    $grid->column('images', '封面')->display(function ($value) {
		       return img_path($value);
        })->lightbox(['width' => 50, 'height' => 50]);
         $grid->column('source', '来源')->display(function ($value) {
	       if ($value === 'huxiu') {return _label('虎嗅', 'info');}
	       if ($value === 'thepaper') {return _label('澎湃', 'danger');} 
	       if ($value === '36ke') {return _label('36氪', 'success');} 
        });
        $grid->column('is_examine', '是否审核')->bool();
         $grid->column('started_at', '新闻发布时间');
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
	   	$this->gridFilter($grid);
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->add(new NewsExamine());
			$actions->add(new NewsExamineNone());
			$actions->add(new NewsTranslate());
			/*$actions->add(new NewsBriefCreate());*/
			//$actions->add(new RefreshBlindBoxRate());
		});

		$grid->batchActions(function ($batch) {
		    $batch->add(new BatchNewsExamine());
		    $batch->add(new BatchTranslate());
		});
	}
	
	 protected function gridFilter(Grid $grid)
    {
	    $grid->filter(function (Grid\Filter $filter) {
		    $filter->expand();
	    	$filter->scope('is_examine', '已审核')->where('is_examine', true);
	    	$filter->scope('is_translate', '已翻译')->whereNotNull('title_zn');
	    });
    }

    protected function detail($id)
    {
        $show = new Show(News::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new News());

	   //	$form->select('blind_box_category_id','盲盒分类')->options(BlindBoxCategory::all()->pluck('name', 'id'));

	    $form->text('title_cn', '中文标题');
	     $form->text('title_zn', '英文标题');
 		$form->simditor('content_cn', '中文内容')->rules('required|string');
 		$form->simditor('content_zn', '英文内容');
 		$form->textarea('brief_zn', '英文摘要');
 		$form->textarea('brief', '摘要');
 		$form->image('images', '封面图片')
		    ->uniqueName()
		    ->creationRules('required');

        return $form;
    }

   
}
